package cn.itcast.mq.listener;

import cn.itcast.mq.config.CommonConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class SpringRabbitListener {

    @RabbitListener(queues = CommonConfig.DIRECT_QUEUE)
    public void listenMessage(String msg) {
        log.info("消费者接收到simple.queue的消息：【{}】", msg);
        // 模拟异常
        System.out.println(1 / 0);
        log.debug("消息处理完成！");
    }

//    /**
//     * 处理失败消息的路由模型
//     * @param msg
//     */
//    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "error.queue"),
//            exchange = @Exchange(name = "error.exchange", type = ExchangeTypes.DIRECT),
//            key = {"error.key"}))
//    public void listenErrorMessage(String msg){
//        System.out.println("接收到失败的消息：【" + msg + "】");
//    }


    @RabbitListener(queues = CommonConfig.ERROR_QUEUE)
    public void listenErrorMessage(String msg) {
        System.out.println("接收到失败的消息：【" + msg + "】");
    }
}
